//
//  MBProgressHUD+Loading.m
//  iosCharge
//
//  Created by SX L on 2017/8/25.
//  Copyright © 2017年 sushitong. All rights reserved.
//

#import "MBProgressHUD+Loading.h"

@implementation MBProgressHUD (Loading)

//HUD统一的生成方式
+ (MBProgressHUD *)createHUDInView:(UIView *)view mode:(MBProgressHUDMode)mode
{
    if (@available(iOS 9.0, *)) {
        [UIActivityIndicatorView appearanceWhenContainedInInstancesOfClasses:@[[MBProgressHUD class]]].color = [UIColor whiteColor];
    } else {
        [UIActivityIndicatorView appearanceWhenContainedIn:[MBProgressHUD class], nil].color = [UIColor whiteColor];
    }
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.mode = mode;
//    if (mode == MBProgressHUDModeIndeterminate) {
//        [UIActivityIndicatorView appearanceWhenContainedInInstancesOfClasses:@[[MBProgressHUD class]]].color = [UIColor whiteColor];
//    }
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.bezelView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.8];
    hud.detailsLabel.textColor = [UIColor whiteColor];
    hud.detailsLabel.font = [UIFont systemFontOfSize:15];
    hud.removeFromSuperViewOnHide = YES;
    return hud;
}

//文字提示HUD统一的生成方式
+ (void)showMsgInView:(UIView *)view msg:(NSString *)msg delay:(NSTimeInterval)delay
{
    MBProgressHUD *hud = [self createHUDInView:view mode:MBProgressHUDModeText];
    
    if ([self isNullOrNil:msg])
    {
        hud.detailsLabel.text = @"请求有误!";
    }
    else
    {
        delay = msg.length > 20 ? 3.0 : delay;
        hud.detailsLabel.text = msg;
    }
    hud.layer.cornerRadius = 5;
    hud.margin = 8;
    hud.userInteractionEnabled = NO;
    [hud hideAnimated:YES afterDelay:delay];
}

//添加提示文字
+ (void)showMsgInView:(UIView *)view msg:(NSString *)msg {
    [self showMsgInView:view msg:msg delay:2.0];
}

+ (void)showMsgInWindow:(NSString *)msg {
    [self showMsgInView:[UIApplication sharedApplication].delegate.window msg:msg delay:2.0];
}

//添加提示菊花
+ (void)showLoadingInView:(UIView *)view msg:(NSString *)msg {
    MBProgressHUD *hud = [self createHUDInView:view mode:MBProgressHUDModeIndeterminate];
    
    hud.detailsLabel.text = msg;
    hud.userInteractionEnabled = NO;
}

+ (void)showLoadingInView:(UIView *)view {
    MBProgressHUD *hud = [self createHUDInView:view mode:MBProgressHUDModeIndeterminate];
    hud.userInteractionEnabled = NO;
}

+ (void)showLoadingInWindow {
    [self createHUDInView:[UIApplication sharedApplication].delegate.window mode:MBProgressHUDModeIndeterminate];
}

//带图片的提示
+ (void)showSuccessMsg:(NSString *)msg {
    [self showCustomIconInView:[UIApplication sharedApplication].delegate.window icon:@"MBHUD_Success" msg:msg];
}

+ (void)showErrorMsg:(NSString *)msg {
    [self showCustomIconInView:[UIApplication sharedApplication].delegate.window icon:@"MBHUD_Error" msg:msg];
}

+ (void)showInfoMsg:(NSString *)msg {
    [self showCustomIconInView:[UIApplication sharedApplication].delegate.window icon:@"MBHUD_Info" msg:msg];
}

+ (void)showWarnMsg:(NSString *)msg {
    [self showCustomIconInView:[UIApplication sharedApplication].delegate.window icon:@"MBHUD_Warn" msg:msg];
}

//隐藏view中所有的HUD
+ (void)hideHUDForView:(UIView *)view {
//    [self hideAllHUDsForView:view animated:YES];
    [self hideHUDForView:view animated:YES];
}

+ (void)hideHUDForWindow {
    [self hideHUDForView:[UIApplication sharedApplication].delegate.window animated:YES];
}

+ (void)showCustomIconInView:(UIView *)view icon:(NSString *)icon msg:(NSString *)msg {
    MBProgressHUD *hud = [self createHUDInView:view mode:MBProgressHUDModeCustomView];
    hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:icon]];
    hud.detailsLabel.text = msg;
    hud.detailsLabel.textColor = [UIColor whiteColor];
    [hud hideAnimated:YES afterDelay:2.0];
}

+ (BOOL)isNullOrNil:(id)object {
    if (object == nil || [object isEqual:[NSNull null]]) {
        return YES;
    } else if ([object isKindOfClass:[NSString class]]) {
        if ([object isEqualToString:@""]) {
            return YES;
        } else {
            return NO;
        }
    } else if ([object isKindOfClass:[NSNumber class]]) {
        if ([object isEqualToNumber:@0]) {
            return YES;
        } else {
            return NO;
        }
    }
    return NO;
}

@end
